package cn.tedu.kcircle.content.front.adapter.controller;

import cn.tedu.kcircle.common.po.CurrentPrincipal;
import cn.tedu.kcircle.common.restful.JsonResult;
import cn.tedu.kcircle.content.front.adapter.converter.ContentConverter;
import cn.tedu.kcircle.content.front.protocol.param.ContentAddParam;
import cn.tedu.kcircle.content.front.service.ContentFrontService;
import cn.tedu.kcircle.content.front.service.bo.ContentAddBO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("content")
@Tag(name="内容管理")
@AllArgsConstructor
public class ContentFrontController {
    private ContentFrontService contentService;
    private ContentConverter converter;
    @PostMapping("add/article")
    @Operation(summary = "发布文章")
    public JsonResult<Void> addNew(@AuthenticationPrincipal CurrentPrincipal principal,
                             @Validated @ParameterObject ContentAddParam contentAddParam){
        log.debug("开始处理【发布文章】的请求，参数：{}", contentAddParam);
        ContentAddBO contentAddBO = converter.param2BO(contentAddParam);
        contentAddBO.setIp(principal.getUserAddr());
        contentAddBO.setAuthorName(principal.getUsername());
        contentAddBO.setAuthorId(principal.getUserId());
        contentService.addArticle(contentAddBO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @Operation(summary = "根据ID删除文章")
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id) {
        log.debug("开始处理【根据ID删除文章】的请求，参数：{}", id);
        contentService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/dynamic")
    @Operation(summary = "查找用户动态")
    public JsonResult list(@AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        log.debug("开始处理【根据ID删除文章】的请求，参数：{}", currentPrincipal);
        List<Object> queryResult= contentService.getUserActivities(currentPrincipal);
        return JsonResult.ok(queryResult);
    }

}
